package com.jubao.ejb.business.impl;

import java.util.List;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;

import org.jboss.ejb3.annotation.LocalBinding;
import org.jboss.ejb3.annotation.RemoteBinding;

import com.jubao.ejb.business.Business;
import com.jubao.ejb.business.StoreBusinessLocal;
import com.jubao.ejb.business.StoreBusinessRemote;
import com.jubao.ejb.business.util.BusinessConstant;
import com.jubao.ejb.entity.Category;
import com.jubao.ejb.entity.Coupon;
import com.jubao.ejb.entity.CouponItem;
import com.jubao.ejb.entity.GeneRelation;
import com.jubao.ejb.entity.Store;
import com.jubao.ejb.entity.StoreTransaction;
import com.jubao.ejb.entity.User;

@Stateless
@LocalBinding(jndiBinding = BusinessConstant.StoreBusinessLocalJndiName)
@RemoteBinding(jndiBinding = BusinessConstant.StoreBusinessRemoteJndiName)
public class StoreBusinessImpl extends Business implements StoreBusinessLocal, StoreBusinessRemote {

	public List<Store> getStoreList(long areaCategorySysId, long businessCategorySysId, int pgno, int pgsize) {
		int relationId1 = areaCategorySysId > -1 ? BusinessConstant.RelationID.STORE_IS_MEMBER_OF_CATEGORY.getId() : -1;
		int relationId2 = businessCategorySysId > -1 ? BusinessConstant.RelationID.STORE_IS_MEMBER_OF_CATEGORY.getId()
				: -1;

		List stores = super.geneRelationDao.getEntityList(Store.class.getName(), relationId1, areaCategorySysId,
				relationId2, businessCategorySysId, pgno, pgsize);
		return stores;
	}

	public long getStoreCount(long areaCategorySysId, long businessCategorySysId) {
		int relationId1 = areaCategorySysId > -1 ? BusinessConstant.RelationID.STORE_IS_MEMBER_OF_CATEGORY.getId() : -1;
		int relationId2 = businessCategorySysId > -1 ? BusinessConstant.RelationID.STORE_IS_MEMBER_OF_CATEGORY.getId()
				: -1;

		Long result = super.geneRelationDao.getEntityCount(Store.class.getName(), relationId1, areaCategorySysId,
				relationId2, businessCategorySysId);
		return result;
	}

	public User findServant(long storeSysId) {
		List users = geneRelationDao.getEntityList(User.class.getName(),
				BusinessConstant.RelationID.USER_IS_SERVANT_OF_STORE.getId(), storeSysId, 0, 1);
		if (users != null) {
			return (User) users.get(0);
		}
		return null;
	}

	public GeneRelation setServant(long storeSysId, long userSysId) {
		Store store = storeDao.find(storeSysId);
		User user = userDao.find(userSysId);
		GeneRelation gr = super.geneRelationDao.find(userSysId, BusinessConstant.RelationID.USER_IS_SERVANT_OF_STORE
				.getId(), storeSysId);
		if (store != null && user != null) {
			if (gr == null) {
				gr = new GeneRelation();
				gr.setEntity(user);
				gr.setRelationId(BusinessConstant.RelationID.USER_IS_SERVANT_OF_STORE.getId());
				gr.setEntity2(store);
			} else {
				gr.setEntity(user);
			}
			gr = geneRelationDao.save(gr);
		}
		return gr;
	}

	/* (non-Javadoc)
	 * @see com.jubao.ejb.business.StoreBusiness#add2CateogryTrail(long, long)
	 */
	public void add2CateogryTrail(long storeSysId, long categorySysId) {
		Store store = storeDao.find(storeSysId);
		List<Category> primaryCategoryTrail = categoryDao.getPrimaryCategoryTrail(categorySysId);
		if (store != null && primaryCategoryTrail != null && primaryCategoryTrail.size() > 0) {
			for (int i = 0; i < primaryCategoryTrail.size(); i++) {
				Category category = primaryCategoryTrail.get(i);
				GeneRelation gr = geneRelationDao.find(storeSysId,
						BusinessConstant.RelationID.STORE_IS_MEMBER_OF_CATEGORY.getId(), category.getSysId());
				if (gr == null) {
					gr = new GeneRelation();
					gr.setEntity(store);
					gr.setRelationId(BusinessConstant.RelationID.STORE_IS_MEMBER_OF_CATEGORY.getId());
					gr.setEntity2(category);
					geneRelationDao.save(gr);
				}
			}
		}
	}

	/* (non-Javadoc)
	 * @see com.jubao.ejb.business.StoreBusiness#removeFromCategoryTrail(long, long)
	 */
	public void removeFromCategoryTrail(long storeSysId, long categorySysId) {
		Store store = storeDao.find(storeSysId);
		List<Category> primaryCategoryTrail = categoryDao.getPrimaryCategoryTrail(categorySysId);
		if (store != null && primaryCategoryTrail != null && primaryCategoryTrail.size() > 0) {
			for (int i = 0; i < primaryCategoryTrail.size(); i++) {
				Category category = primaryCategoryTrail.get(i);
				GeneRelation gr = geneRelationDao.find(storeSysId,
						BusinessConstant.RelationID.STORE_IS_MEMBER_OF_CATEGORY.getId(), category.getSysId());
				if (gr != null) {
					geneRelationDao.remove(gr);
				}
			}
		}
	}

	public long getCouponCount(long storeSysId) {

		return couponDao.getCouponCount(storeSysId);
	}

	public List<Coupon> getCouponList(long storeSysId, int pgno, int pgsize) {
		return couponDao.getCouponList(storeSysId, pgno, pgsize);
	}

	public long getCouponItemCount(long storeSysId, long couponSysId, long userSysId) {
		return couponDao.getCouponItemCount(storeSysId, couponSysId, userSysId);
	}

	public List<CouponItem> getCouponItemList(long storeSysId, long couponSysId, long userSysId, int pgno, int pgsize) {
		return couponDao.getCouponItemList(storeSysId, couponSysId, userSysId, pgno, pgsize);
	}

	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public CouponItem newCouponItem(long storeSysId, long couponSysId, long customerUserSysId, CouponItem couponItem,
			long operatorUserSysId) {
		CouponItem result = null;
		Coupon coupon = couponDao.findCoupon(couponSysId);
		User customer = userDao.find(customerUserSysId);
		if (coupon != null && coupon.getBonusValue() != null && coupon.getBonusValue() > 0 && customer != null
				&& couponItem != null && coupon.getStore() != null && coupon.getStore().getSysId() == storeSysId) {
			couponItem.setUser(customer);
			couponItem.setCoupon(coupon);
			couponItem.setStatus(BusinessConstant.Status.IS_APPROVING.getId());
			result = couponDao.saveCouponItem(couponItem);
			if (result != null) {
				this.newStoreTransaction(storeSysId, -coupon.getBonusValue(), "New Coupon Item", result.getSysId(),
						operatorUserSysId);
			}
		}
		return result;
	}

	public CouponItem confirmCouponItem(long userSysId, CouponItem couponItem) {
		CouponItem result = null;
		User user = userDao.find(userSysId);
		if (user != null && couponItem != null && couponItem.getUser() != null
				&& couponItem.getUser().getSysId() == userSysId) {
			couponItem.setStatus(BusinessConstant.Status.IS_APPROVE_SUCC.getId());
			result = couponDao.saveCouponItem(couponItem);
		}
		return result;
	}

	public long getStoreTransactionCount(long storeSysId) {
		return storeTransactionDao.getStoreTransactionCount(storeSysId);
	}

	public List<StoreTransaction> getStoreTransactionList(long storeSysId, int pgno, int pgsize) {
		return storeTransactionDao.getStoreTransactionList(storeSysId, pgno, pgsize);
	}

	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public StoreTransaction newStoreTransaction(long storeSysId, int value, String shortDesc, long couponItemSysId,
			long operatorUserSysId) {
		StoreTransaction result = null;
		Store store = storeDao.find(storeSysId);
		User operator = userDao.find(operatorUserSysId);
		if (store != null && operator != null) {
			CouponItem couponItem = couponDao.findCouponItem(couponItemSysId);
			StoreTransaction ltx = storeTransactionDao.findLatest(storeSysId);
			int balance = 0;
			if (ltx != null) {
				balance = ltx.getBalance();
			}
			result = new StoreTransaction();
			result.setValue(value);
			result.setBalance(balance + value);
			result.setShortDesc(shortDesc);
			result.setStore(store);
			result.setCouponItem(couponItem);
			result.setOperator(operator);
			storeTransactionDao.save(result);
		}
		return result;
	}
}
